package com.kehutong.repair.admin;

import java.util.Arrays;
import java.util.List;

import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import com.kehutong.repair.entity.Repairs;
import com.kehutong.repair.entity.RollOutDetail;
import com.kehutong.repair.enums.RepairsStatus;
import com.kehutong.repair.enums.Source;
import com.kehutong.repair.service.admin.AdminRepairsService;

@MvcAction
@ReqMapping("/admin/repairs")
public class AdminRepairsController {
	
	static final Logger logger = LoggerFactory.getLogger(AdminRepairsController.class);

    @Inject
    private DoveClient doveClient;
    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private AdminRepairsService adminRepairsService;

    @HasPermission("admin:repairs:view")
    @ReqMapping("/list")
    public Object list(Token token, JSONObject jsonObject) throws Exception {
        return Result.success(adminRepairsService.list(token, jsonObject));
    }

    @HasPermission("admin:repairs:edit")
    @ReqMapping("/update")
    public Object update(JSONObject jsonObject, Token token) throws Exception {
        Repairs repairs = Pool.get(Repairs.class, jsonObject.getString("id"));
        return adminRepairsService.update(token, jsonObject, repairs);
    }

    @HasPermission("admin:repairs:view")
    @ReqMapping("/get")
    public Object get(Token token, String id) throws Exception {
        Repairs item = adminRepairsService.get(token, id);
        if (item == null) {
            return Result.error(0, "对象不存在");
        }
        return Result.success(item);
    }

    @HasPermission("admin:repairs:view")
    @ReqMapping("/enums")
    public Object loadEnums() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("repairsStatusList", Arrays.asList(RepairsStatus.values()));
        jsonObject.put("sourceList", Arrays.asList(Source.values()));

        return Result.success(jsonObject);
    }

    @HasPermission("admin:repairs:view")
    @ReqMapping("/getRoll")
    public Object getRoll(String id) {
        List<RollOutDetail> list = adminRepairsService.getRoll(id);
        return Result.success(list);
    }

    @HasPermission("admin:repairs:edit")
    @ReqMapping("/remark")
    public Object remark(Token token, JSONObject jsonObject) throws Exception {
        return adminRepairsService.remark(token, jsonObject);
    }

    @HasPermission("admin:repairs:view")
    @ReqMapping("/get/remarkList")
    public Object remarkList(String id){
        return Result.success(adminRepairsService.remarkList(id));
    }

    @HasPermission("admin:repairs:edit")
    @ReqMapping("/cancel")
    public Object cancel(Token token, JSONObject jsonObject) throws Exception {
        return adminRepairsService.cancel(token, jsonObject);
    }

    @HasPermission("admin:repairs:edit")
    @ReqMapping("/delete")
    public Object delete(JSONObject jsonObject) {
        return adminRepairsService.delete(jsonObject);
    }

    @HasPermission("admin:repairs:edit")
    @ReqMapping("/finish")
    public Object finish(Token token, JSONObject jsonObject) throws Exception {
        return adminRepairsService.finish(token, jsonObject);
    }

    @HasPermission("admin:repairs:edit")
    @ReqMapping("/change")
    public Object change(Token token, JSONObject jsonObject) throws Exception {
        return adminRepairsService.change(token, jsonObject);
    }

    @HasPermission("admin:repairs:edit")
    @ReqMapping("/again")
    public Object again(Token token, JSONObject jsonObject) throws Exception {
        return adminRepairsService.again(token, jsonObject);
    }

    @HasPermission("admin:repairs:edit")
    @ReqMapping("/accept")
    public Object acceptRepairs(Token token, JSONObject jsonObject) throws Exception {
        return adminRepairsService.acceptRepairs(token, jsonObject);
    }
}
